$(function () {
    $("#loginButton").click(function () {
        var username = $("#username").val();
        var password = $("#password").val();
        var captchaCodeValue = $("#captchaCodeValue").val();
        if (username == null || username == "") {
            layer.msg("帐号不能为空,请输入!");
            return false;
        }
        if (password == null || password == "") {
            layer.msg("帐号不能为空,请输入!");
            return false;
        }
        if (username.length > 30) {
            layer.msg("密码的最大长度为30个字符!");
            return false;
        }
        if (password.length > 20) {
            layer.msg("密码的最大长度为20个字符!");
            return false;
        }
        if (captchaCodeValue == null || captchaCodeValue == "") {
            layer.msg("验证码不能为空,请输入!");
            return false;
        }
        if (captchaCodeValue.length != 5) {
            layer.msg("验证码输入错误,请重新输入!");
            return false;
        }
        if ($.cookie('captchaCodeKey') != null && $.cookie('captchaCodeKey') != undefined) {
            $("#captchaCodeKey").val($.cookie('captchaCodeKey'));
        }

        $.ajax({
            type: "post",
            url: '/login/loginUser',
            data: $('#loginFrom').serialize(),
            success: function (data) {
                var data = eval("(" + data + ")");
                if (data.success == true) {
                    window.location.href = "/login/indexInfo?userId=" + data.userId;
                } else {
                    layer.msg(data.msg);
                }
            }
        });
    })

    $("#captchaCode").click(function () {
        $("#captchaCode").attr("src", "/login/getcaptcha?" + Math.random())
    })
});
//打开页面后用户名获取焦点
$("#username").focus();

function butOnClick() {
    //谷歌和IE能识别window.event，火狐使用arguments.callee.caller.arguments[0]
    var theEvent = window.event || arguments.callee.caller.arguments[0];
    var code = theEvent.keyCode;
    if (code == 13) {
        var button = document.getElementById("loginButton"); //bsubmit 为botton按钮的id
        button.click();
        return false;
    }
}

